package com.wyhello.tube4.spring.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wyhello.tube4.pojo.Reside;
import com.wyhello.tube4.pojo.Tube;
import com.wyhello.tube4.spring.model.ResideRecordsModel;
@Controller
public class MapController {
	@Autowired
	private ResideRecordsModel resideRecordsModel;
	/**
	 * 查询锅炉的信息
	 * @param reside
	 * @return
	 */
	@RequestMapping("/map/boiler")
	public ModelAndView queryBoiler(){
			List<Reside> boilers =  resideRecordsModel.queryResideByBoiler();
			ModelAndView model = new ModelAndView("boiler","boilers",boilers);
			return model;
	}
	
	
	/**
	 * 查询指定管排下的管
	 * @param parentid
	 * @return
	 */
	@RequestMapping("/map/queryTube")
	public void queryTube(int parentid,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(parentid);
		Set<Tube> tubes = reside.getTubes();
		Map<Integer,String> map = new TreeMap<Integer,String>();
		for (Tube tube : tubes) {
			map.put(tube.getId(), tube.getName());
		}
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}
	
	/**
	 * 根据id查询Reside
	 * @param reside
	 * @return
	 */
	@RequestMapping("/map/queryResideById")
	public ModelAndView queryResideById(int id,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(id);
		ModelAndView model = new ModelAndView("row","row",reside);
		return model;
	}
	/*@RequestMapping("/map/queryResideById")
	public void queryResideById(int id,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(id);
		Map<String,String> map = new HashMap<String,String>();
		
		if(reside != null && reside.getAssignImagePath() != null){
			map.put("assignImagePath", reside.getAssignImagePath());
		}else{
			map.put("assignImagePath", "");
		}
		
		if(reside != null && reside.getDescImagePath() != null){
			map.put("descImagePath", reside.getDescImagePath());
		}else{
			map.put("descImagePath", "");
		}
		
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}*/
	
	/**
	 * 根据管ID查询管信息
	 * @param id
	 * @param uil
	 * @return
	 */
	@RequestMapping("/map/getTubeById")
	public ModelAndView getTubeById(int id){
		Tube tube = resideRecordsModel.getTubeById(id);
		ModelAndView model = new ModelAndView("tube","tube",tube);
		return model;
	}
	
}
